using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Net;
using System.IO;

namespace SeeqSharp
{
    public class Track
    {
        private const string DownloadUri = "http://www.seeqpod.com/api/youtube.com/results?mp3_url_id={0}";

        private string location = "";
        private string title = "";
        private string creator = "";
        private string album = "";
        private string genre = "";
        private string composer = "";
        private SeeqpodMetadata metadata;

        public Track()
        {
            this.metadata = new SeeqpodMetadata();
        }

        [XmlElement(ElementName = "location")]
        public string Location
        {
            get { return (this.location); }
            set { this.location = value; }
        }

        [XmlElement(ElementName = "title")]
        public string Title
        {
            get { return (this.title); }
            set { this.title = value; }
        }

        [XmlElement(ElementName = "creator")]
        public string Creator
        {
            get { return (this.creator); }
            set { this.creator = value; }
        }

        [XmlElement(ElementName = "album")]
        public string Album
        {
            get { return (this.album); }
            set { this.album = value; }
        }

        [XmlElement(ElementName = "genre")]
        public string Genre
        {
            get { return (this.genre); }
            set { this.genre = value; }
        }

        [XmlElement(ElementName = "composer")]
        public string Composer
        {
            get { return (this.composer); }
            set { this.composer = value; }
        }

        [XmlElement(ElementName = "extension")]
        public SeeqpodMetadata Metadata
        {
            get { return (this.metadata); }
            set { this.metadata = value; }
        }

        public string GetDownloadUri()
        {
            WebClient client = new WebClient();
            string downloadUri = client.DownloadString(string.Format(Track.DownloadUri, metadata.Mp3UrlId));
            if (downloadUri == "not_available")
                downloadUri = null;
            return (downloadUri);
        }

        public override string ToString()
        {
            string fullTitle = this.Title;
            if (!string.IsNullOrEmpty(this.Creator))
            {
                fullTitle += " by " + this.Creator;
            }
            return fullTitle;
        }
    }
}
